// ==UserScript==
// @name           Filtrowanie rozkazów
// @namespace      http://code.google.com/p/plemiona-skrypty/
// @description    Filtrowanie rozkazów - bez wykrywania fake'ów
// @version        1.9.0001
// @license        Creative Commons 3.0 BY-SA (http://creativecommons.org/licenses/by-sa/3.0/deed.pl)
// @author         szatdafakp - Forum Plemion
// @contributor    Lukasz032 - plemiona Ś7
// @contributor    Nexces
// @include        http://pl*.plemiona.pl/game.php*screen=overview_villages*mode=comm*
// @include        http://pl*.plemiona.pl/game.php*screen=overview_villages*mode=inco*
// @include        http://pl*.plemiona.pl/game.php*screen=info_village*
// ==/UserScript==

if (window.opera) {
	unsafeWindow = window;
}

function trim(str, chars) {
    return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function addListBox(villages) {
	var tables = document.getElementsByTagName('table');
	var tab;
	for (var i = 0; i < tables.length; i++) {
		if (tables[i].getElementsByTagName('tr').length > 0 && tables[i].getAttribute("class") == "vis") {
			if (trim(tables[i].getElementsByTagName('tr')[0].textContent.substring(0,6)) == "Stare" || trim(tables[i].getElementsByTagName('tr')[0].textContent).substring(0,9) == "Wszystkie") {
				tab = tables[i];
			}
		}
	}
	var cells = tab.getElementsByTagName('tr')[0].getElementsByTagName('td');
	for (var i = 0; i < cells.length; i++) {
		cells[i].setAttribute('width', '20%');
	}
	var td = document.createElement('td');
	td.setAttribute('width','20%');
	var select = document.createElement('select');
	tab.getElementsByTagName('tr')[0].appendChild(td);
	var option = document.createElement('option');
	option.text = "Wszystkie"
	option.value = "Wszystkie";
	select.add(option,null);
	for (var i = 0; i < villages.length; i++) {
		option = document.createElement('option');
		option.text = villages[i];
		option.value = villages[i];
		select.add(option,null);
	}
	select.setAttribute("onchange", "filter(this.value)");
	select.setAttribute("onkeyup", "filter(this.value)");
	td.appendChild(select);

	return select;
}

unsafeWindow.filter = function filter(village) {
	var ordersTable;
	var tables = document.getElementsByTagName('table');
	for (var i = 0; i < tables.length; i++) {
		if (tables[i].getElementsByTagName('tr').length > 0) {
			if (tables[i].getElementsByTagName('tr')[0].textContent.substring(0,6) == "Rozkaz") {
				ordersTable = tables[i];
				break;
			}
		}
	}
	var rows = ordersTable.getElementsByTagName('tr');
	var rowAClass ="nowrap row_a";
	var rowBClass ="nowrap row_b";
	var rowNumber = 0;

	for (var i=1; i < rows.length; i++) {
		var order = rows[i].textContent;
		order = trim(order);
		if ((village == null || order.indexOf(village) < 0) && village != "Wszystkie") {
			rows[i].style.display="none";
		} else {
			rows[i].style.display="";
			rows[i].className = (rowNumber % 2 == 0 ? rowAClass : rowBClass);
			rowNumber++;
		}
	}
}


function addLinks() {
	var tables = document.getElementsByTagName('table');
	for (i = tables.length-1; i >= 0; i--) {
		if ( tables[i].getElementsByTagName('tr').length > 1 && trim(tables[i].getElementsByTagName('tr')[1].textContent).substring(0, 11) == "Współrzędne") {
			var coords = tables[i].getElementsByTagName('tr')[1].getElementsByTagName('td')[1].textContent;
			var newRow = document.createElement("tr");
			var newCell = document.createElement("td");
			newCell.setAttribute("colspan", "2");
			newCell.innerHTML = "<a href='/game.php?screen=overview_villages&page=-1&group=0&mode=commands&type=attack&coords=" + coords + "'>» Pokaż ataki wysłane na wioskę</a>"
			newRow.appendChild(newCell);
			tables[i].getElementsByTagName('tbody')[0].appendChild(newRow);

			newRow = document.createElement("tr");
			newCell = document.createElement("td");
			newCell.setAttribute("colspan", "2");
			newCell.innerHTML = "<a href='/game.php?screen=overview_villages&page=-1&group=0&mode=commands&type=support&coords=" + coords + "'>» Pokaż wsparcia wysłane do wioski</a>"
			newRow.appendChild(newCell);
			tables[i].getElementsByTagName('tbody')[0].appendChild(newRow);

			break;
		}
	}
}


function createVillagesList() {
	var ordersTable;
	var tables = document.getElementsByTagName('table');
	for (i = 0; i < tables.length; i++) {
		if (tables[i].getElementsByTagName('tr').length > 0) {
			if (tables[i].getElementsByTagName('tr')[0].textContent.substring(0,6) == "Rozkaz") {
				ordersTable = tables[i];
				break;
			}
		}
	}
	var rows = ordersTable.getElementsByTagName('tr');
	var villages = new Array(0);
	for (i = 1; i < rows.length; i++) {
		var order = trim(rows[i].getElementsByTagName('td')[0].textContent);
		if (document.URL.indexOf("mode=inco") >= 0) {
			var target = rows[i].cells[1].textContent;
		}
		var village;
		if (order.substring(0,7) == "Atak na") {
			village = order.substr(8);
		} else if (order.substring(0,7) == "Odesłan") {
			village = order.substr(10);
		} else if (order.substring(0,7) == "Powrót ") {
			village = order.substr(9);
		} else if (order.substring(0,7) == "Wsparci") {
			village = order.substr(13);
		} else if (order.substring(0,7) == "Odwrót ") {
			village = order.substr(9);
		} else if (order.substring(0,9) == "Przerwana") {
			village = order.substr(20);
		} else if (order.substring(0,4) == "Atak" && document.URL.indexOf("mode=inco") >= 0) {
			village = target;
		} else if (order.substring(0,5) == "Pomoc" && document.URL.indexOf("mode=inco") >= 0) {
			village = target;
		}
		var j = 0;
		for (j = 0; j < villages.length; j++) {
			if (villages[j] == village) {
				break;
			}
		}
		if (j == villages.length) {
			villages[villages.length] = village;
		}
	}
	return villages;
}

// entry point
if (document.URL.indexOf("mode=comm") >= 0 || document.URL.indexOf("mode=inco") >= 0) {
	var listbox = addListBox(createVillagesList());
	if (document.URL.indexOf("coords") >= 0) {
		var coords = document.URL.substring(document.URL.indexOf("coords") + 7);
		var options = listbox.options;
		var i;
		for (i = 0; i < options.length; i++) {
			if (options[i].textContent.indexOf(coords) >= 0) {
				listbox.selectedIndex = i;
				filter(listbox.value);
				break;
			}
		}
		if (i == options.length) {
			if ( document.URL.indexOf("type=support") >= 0) {
				alert("Nie wysłałeś żadnych wsparć do wybranej wioski");
			} else if ( document.URL.indexOf("type=attack") >= 0 ) {
				alert("Nie wysłałeś żadnych ataków na wybraną wioskę");
			}
		}
	}
} else if (document.URL.indexOf("screen=info_village")) {
	addLinks();
}
